from flask import Blueprint, flash, session, request
from flask import render_template, redirect, url_for

from core.login.form import LoginForm, is_login, submit_login, is_register, submit_register

login_blueprint = Blueprint('login', __name__)


@login_blueprint.route('/login', methods=['GET', 'POST'])
def login():
    form = LoginForm()

    if request.method == 'POST':
        if is_login(form):
            if submit_login(form):
                flash('登录成功')
                return redirect(url_for('personal.personal'))
            else:
                flash('检查账号或密码')

        if is_register(form):
            if submit_register(form):
                flash('注册账号成功')
            else:
                flash('换个账号名再试试呢')

    return render_template('page/login/index.html', form=form)
